from main.models import FriendNotice,User
from main import db
from sqlalchemy.exc import InvalidRequestError
from main.utils import JSONHelper
from datetime import datetime
from sqlalchemy import or_, and_
from flask import render_template


def insert(friendnotice):
    try:
        friendnotice.time = datetime.now()
        db.session.add(friendnotice)
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False


# 传入用户名查询所有好友的名字
def query_user_id_is_notice(user_id,is_notice):
    return db.session.query(FriendNotice).filter(and_(FriendNotice.user_id == user_id, FriendNotice.is_notice == is_notice)).first()


def update(friendnotice):
    try:
        friendnotice.time = datetime.now()
        db.session.commit()
        return True
    except InvalidRequestError:
        db.session.rollback()
    except Exception as e:
        print(e)
        return False
